package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;
import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import system.Device;
import system.GameOption;
import system.Graphics;
import system.KeyBoard;

import com.game.GamePlay;
import com.game.R;

public class Splash extends State {

	private GL10 gl;
	private int m_fSrcWidth;
	private int m_fSrcHeight;

	
	public void Init() {
		gl = Graphics.GetGraphics();
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		
		float w = m_fSrcWidth/2;
		float h = m_fSrcHeight/2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		
		LoadTexture();
		// init background
		Device.DisableScaleFactor();
		m_background = new TexSquare(Device.GetWidth() , Device.GetHeight());
		Device.EnableScaleFactor();
		m_pressanykey = new TexSquare(m_iPressAnyKeyWidth, m_iPressAnyKeyHeight);
		m_title = new TexSquare(m_iTitleWidth, m_iTitleHeight);
		
		m_iPressAnyKeyOffset = (m_fSrcHeight >> 1) - m_iPressAnyKeyHeight;
		m_iTitleOffset = m_fSrcHeight >> 2;
		
		if(GameOption.isMusicOn)
		{
			GameSound.GetBgSound().PlayMainMenu();
		}
		
		m_iStartTime = System.nanoTime();
	}
	
	private void LoadTexture()
	{		
		// Background
		m_texture = new Texture();
		m_iBackgroundTexID = m_texture.initTexture(R.drawable.splashbackground);
		
		// Press any key to continue
		m_iPressAnyKeyTexID = m_texture.initTexture(R.drawable.pressanykeytocontinue);
		
		
		// Title		
		m_iGameTitleID = m_texture.initTexture(R.drawable.gamename);
	}

	
	public void Update(GamePlay gamePlay) {
		
		super.Update(gamePlay);
		// Update Press any key
		if(m_isPressAnyKeyVisible)
		{
			m_iEndTime = System.nanoTime();
			if(m_iEndTime - m_iStartTime >= SLEEPTIME_PRESS_ANY_KEY)
			{
				m_isPressAnyKeyVisible = false;
				m_iStartTime = System.nanoTime();
			}
		}
		else
		{
			m_iEndTime = System.nanoTime();
			if(m_iEndTime - m_iStartTime >= SLEEPTIME_PRESS_ANY_KEY)
			{
				m_isPressAnyKeyVisible = true;
				m_iStartTime = System.nanoTime();
			}
		}
	}

	
	private int m_iBackgroundTexID;
	private int m_iPressAnyKeyTexID;
	private int m_iGameTitleID;
	private int m_iPressAnyKeyWidth = 380;
	private int m_iPressAnyKeyHeight = 45;
	private int m_iTitleWidth = 350;
	private int m_iTitleHeight = 60;
	private TexSquare m_background;
	private TexSquare m_pressanykey;
	private TexSquare m_title;
	
	private Texture m_texture;
	
	private int m_iPressAnyKeyOffset;
	private int m_iTitleOffset;
	
	private static final int SLEEPTIME_PRESS_ANY_KEY = 500000000;
	private boolean m_isPressAnyKeyVisible = false;
	private long m_iStartTime;
	private long m_iEndTime;
	
	public void Render() {		
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		
		// Background
		m_texture.loadTexture(m_iBackgroundTexID);
		m_background.Render();
		
		// Title
		m_texture.loadTexture(m_iGameTitleID);
		gl.glPushMatrix();
		gl.glTranslatef(0, m_iTitleOffset, 0);
		m_title.Render();
		gl.glPopMatrix();
		
		// Press any key
		if(m_isPressAnyKeyVisible)
		{
			m_texture.loadTexture(m_iPressAnyKeyTexID);
			gl.glPushMatrix();
			gl.glTranslatef(0, -m_iPressAnyKeyOffset, 0);
			m_pressanykey.Render();
			gl.glPopMatrix();
		}
	}
	
	public void Destroy() {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPopMatrix();
		
		m_background = null;
		m_pressanykey = null;
		m_title = null;
		m_texture = null;
	}

	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
		if(KeyBoard.IsAnyKeyPressed())
		{
			gamePlay.SwitchState(GameState.MainMenu);
		}
	}
}
